var del = require("del");
var gulp = require('gulp');
var rename = require('gulp-rename');/*重命名*/
var lightmin = require('gulp-lightmin');/*压缩html*/
var templateCache = require('gulp-angular-templatecache');/*写入html模版文件到js中*/
var concat = require('gulp-concat');/*合并js*/
var ngAnnotate = require('gulp-ng-annotate');/*ng依赖注入*/
var uglify = require('gulp-uglify');/*js压缩*/
var browserSync = require('browser-sync');/*自动刷新*/
var inject = require('gulp-inject');/*编译模版*/
var autoprefixer = require('gulp-autoprefixer');/*自动添加css浏览器厂商私有前缀*/
var stylus = require('gulp-stylus');/*编译styl预处理css语言*/
var minifyCss = require('gulp-minify-css');/*css压缩*/
var clean = require('gulp-clean');/*清空文件夹*/
var rev = require('gulp-rev'); /*对文件名加MD5后缀*/
var revCollector = require('gulp-rev-collector');/*路径替换*/
var runSequence = require('gulp-run-sequence');  /*同步*/
var preprocess = require('gulp-preprocess');   /*文本替换*/
var replace = require('gulp-replace');
var ngHtml2Js = require("gulp-ng-html2js");
var md5 = require("gulp-md5-plus");


var v= '1.0.1';
var path_v = 'src';  //开发目录
var path_m = 'dist'; //打包目录
var pluginFiles = [
    'node_modules/angular/angular.min.js',
    'node_modules/angular-resource/angular-resource.min.js',
    'node_modules/angular-animate/angular-animate.min.js',
    'node_modules/angular-ui-router/release/angular-ui-router.min.js',
    'node_modules/angular-loading-bar/build/loading-bar.min.css',
    'node_modules/angular-loading-bar/build/dist/loading-bar.min.js',
    'node_modules/angular-ui-bootstrap/dist/ui-bootstrap-csp.css',
    'node_modules/angular-ui-bootstrap/dist/ui-bootstrap.js',
    'node_modules/angular-ui-bootstrap/dist/ui-bootstrap-tpls.js',
    'node_modules/jquery/dist/jquery.min.js',
    'node_modules/bootstrap/**/*',
];
/*生成index模版*/
var jsCss = [
    path_v+ '/styles/*.css',
    path_v+ '/assets/plugins/angular.min.js',
    path_v+ '/assets/plugins/jquery.min.js',
    path_v+ '/assets/plugins/**/*',
    '!'+path_v+ '/js/components/**/*.js',
    path_v+ '/js/template.js'
];
/*生成js模版*/
var buildJs = [
    path_v+ '/js/app.js',
    path_v+ '/js/template.min.js',
    path_v+ '/js/router/**/*.js',
    path_v+ '/js/filter/**/*.js',
    path_v+ '/js/directive/**/*.js',
    path_v+ '/js/components/**/ser-*.js',
    path_v+ '/js/components/**/con-*.js'
];
/*生成打包index模版*/
var buildIndex = [
    path_v+ '/styles/*.css',
    path_v+ '/assets/plugins/angular.min.js',
    path_v+ '/assets/plugins/*'
];
/*图片替换路径*/
var img_url = 'assets/images/';
var img_url_new = 'assets/images/';
/*=========================================        plugins          ================================================*/
gulp.task('plugins', function () {
    return gulp.src(pluginFiles)
        .pipe(gulp.dest(path_v+ '/assets/plugins/'));
});
/*========================================      dev         ==================================================*/
/*生成index模版*/
gulp.task('dev:index', function () {
    var target = gulp.src(path_v+ '/tpl.html');
    var sources = gulp.src(jsCss, {read: false});
    var components = gulp.src([
        path_v+ '/js/router/**/*.js',
        path_v+ '/js/filter/**/*.js',
        path_v+ '/js/directive/**/*.js',
        path_v+ '/js/components/**/fil-*.js',
        path_v+ '/js/components/**/dir-*.js',
        path_v+ '/js/components/**/ser-*.js',
        path_v+ '/js/components/**/con-*.js'
    ], {read: false});
    return target.pipe(inject(sources,{
            ignorePath: path_v+ '/',
            addRootSlash: false,
            removeTags: true/*,
            starttag: '<!-- inject:plugins:{{ext}} -->'*/}))
        .pipe(inject(components,{
            ignorePath: path_v+ '/',
            addRootSlash: false,
            removeTags: true,
            starttag: '<!-- inject:components:{{ext}} -->'}))
        .pipe(rename("index.html"))
        .pipe(gulp.dest(path_v+ '/'));
});
/*css*/
gulp.task('dev:styl', function () {
    return gulp.src([path_v+ '/styles/stylus/index.styl'])
        .pipe(stylus())
        .pipe(autoprefixer({
            browsers: ['last 2 versions'],
            cascade: false
        }))
        .pipe(rename("main.css"))
        .pipe(gulp.dest(path_v+ '/styles/'))
        .pipe(browserSync.reload({stream: true}));
});
/*=====================================        build          =============================================*/
/*压缩js*/
gulp.task('build:template', function () {
    gulp.src([path_v+"/js/**/*.html"])
        .pipe(lightmin())
        .pipe(templateCache({
            root:'js/',  /*设置目录*/
            module:'templates',  /*设置模版模块名称*/
            standalone :false  /*创建一个新的AngularJS模块，而不是使用一个现有的*/
        }))
        .pipe(rename("template.min.js"))
        .pipe(gulp.dest(path_v+ '/js/'));
});
gulp.task('build:js',function () {
    gulp.src(buildJs)
        .pipe(concat("app.js"))
        .pipe(ngAnnotate())
        .pipe(uglify())
        .pipe(rename("app.js"))
        .pipe(gulp.dest(path_m+'/js'));
});
gulp.task('build:assets',function () {
    gulp.src([path_v+ '/assets/**/*','!'+path_v+ '/assets/images/**/*'])
        .pipe(gulp.dest(path_m+'/assets/'))
});
gulp.task('build:ico',function () {
    gulp.src([path_v+'/ico.png'])
        .pipe(gulp.dest(path_m + '/'))
});
gulp.task('build:php',function () {
    gulp.src(['sendMail/**/*'])
        .pipe(gulp.dest(path_m+'/sendMail/'))
});
gulp.task('build:styl',['dev:styl'],function () {
    return gulp.src([path_v+ '/styles/**/*.css'])
        .pipe(replace('../'+img_url,img_url_new))
        .pipe(minifyCss())
        .pipe(gulp.dest(path_m+'/styles'))
});
gulp.task('build:index', function () {
    var target = gulp.src(path_v+ '/tpl.html');
    var sources = gulp.src(buildIndex, {read: false});
    return target.pipe(inject(sources,{
        ignorePath: path_v+ '/',
        addRootSlash: false,
        removeTags: true/*,
         starttag: '<!-- inject:plugins:{{ext}} -->'*/}))
        .pipe(rename("index.html"))
        .pipe(gulp.dest(path_m+ '/'));
});
gulp.task('build:clean', function () {
    return gulp.src([path_m+'/**/*.*'], {read: false})
        .pipe(clean());
});

gulp.task('release:api:index',function () {
    gulp.src([path_m+ '/index.html'])
        .pipe(replace(img_url, img_url_new))
        .pipe(replace(/styles\/(.*?)\.css/g, 'styles/$1.css?v='+v))
        .pipe(replace('js/app.js', 'js/app.js?v='+v))
        .pipe(gulp.dest(path_m+ '/'));
});
gulp.task('release:api', function () {
    runSequence(['release:api:index']);
});
gulp.task('build',function(){
    runSequence('build:clean','build:template',['build:js','build:assets','build:ico','build:styl'],'build:index','release:api','build:php');
    /*browserSync.init({
        server: path_m/!*,
         port: 80*!/
    });*/
});
/*=======================================       server           ===================================================*/
gulp.task('serve',['dev:index','dev:styl'],function() {
    browserSync.init({
        server: path_v/*,
         port: 80*/
    });
    gulp.watch([path_v+ "/js/components/**/*.js",path_v+ "/js/app.js"]).on('change', browserSync.reload);
    gulp.watch([path_v+ '/styles/stylus/**/*.styl'],['dev:styl']);
    gulp.watch([path_v+ "/js/components/**/*.html",path_v+ "/js/directive/**/*.html"]).on('change', browserSync.reload);
    gulp.watch([path_v+ "/tpl.html"],['dev:index']);
    gulp.watch([path_v+ "/index.html"]).on('change', browserSync.reload);
});
/*================================================     release        ========================================================*/

